home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.plaf.basic;
-
- import com.sun.java.swing.AbstractButton;
- import com.sun.java.swing.ButtonModel;
- import com.sun.java.swing.Icon;
- import com.sun.java.swing.JComponent;
- import com.sun.java.swing.LookAndFeel;
- import com.sun.java.swing.SwingUtilities;
- import com.sun.java.swing.UIManager;
- import com.sun.java.swing.plaf.ButtonUI;
- import com.sun.java.swing.plaf.ComponentUI;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Insets;
- import java.awt.Rectangle;
- import java.io.Serializable;
-
- public class BasicButtonUI extends ButtonUI implements Serializable {
- private static final Insets defaultMargin = new Insets(2, 14, 2, 14);
- private static final int defaultTextIconGap = 4;
- private int shift_offset = 0;
- protected static ButtonUI buttonUI;
- protected BasicButtonListener listener;
-
- protected BasicButtonListener createListener(JComponent c) {
- return new BasicButtonListener((AbstractButton)c);
- }
-
- public static ComponentUI createUI(JComponent c) {
- if (buttonUI == null) {
- buttonUI = new BasicButtonUI();
- }
-
- return buttonUI;
- }
-
- public Insets getDefaultMargin(AbstractButton b) {
- return defaultMargin;
- }
-
- public int getDefaultTextIconGap(AbstractButton b) {
- return 4;
- }
-
- protected Color getDisabledColor() {
- return UIManager.getColor("Button.disabled");
- }
-
- protected Color getDisabledTextColor() {
- return UIManager.getColor("Button.disabledText");
- }
-
- protected Color getFocusColor() {
- return UIManager.getColor("Button.focus");
- }
-
- public Dimension getMaximumSize(JComponent c) {
- return this.getPreferredSize(c);
- }
-
- public Dimension getMinimumSize(JComponent c) {
- return this.getPreferredSize(c);
- }
-
- public Dimension getPreferredSize(JComponent c) {
- AbstractButton b = (AbstractButton)c;
- return BasicGraphicsUtils.getPreferredButtonSize(b, 4);
- }
-
- protected Color getSelectColor() {
- return UIManager.getColor("Button.pressed");
- }
-
- private int getTextShiftOffset() {
- return this.shift_offset;
- }
-
- protected void installDefaults(JComponent c) {
- c.setOpaque(false);
- LookAndFeel.installColorsAndFont(c, "Button.background", "Button.foreground", "Button.font");
- LookAndFeel.installBorder(c, "Button.border");
- }
-
- protected void installKeyboardActions(JComponent c) {
- this.listener.setupKeyboard((AbstractButton)c);
- }
-
- protected void installListeners(JComponent c) {
- if (this.listener == null) {
- this.listener = this.createListener(c);
- }
-
- ((Component)c).addMouseListener(this.listener);
- ((Component)c).addMouseMotionListener(this.listener);
- ((Component)c).addFocusListener(this.listener);
- ((AbstractButton)c).addChangeListener(this.listener);
- }
-
- public void installUI(JComponent c) {
- this.installDefaults(c);
- this.installListeners(c);
- this.installKeyboardActions(c);
- }
-
- public void paint(Graphics g, JComponent c) {
- AbstractButton b = (AbstractButton)c;
- ButtonModel model = b.getModel();
- Dimension size = ((Component)b).getSize();
- FontMetrics fm = g.getFontMetrics();
- Insets i = c.getInsets();
- Rectangle viewRect = new Rectangle(size);
- viewRect.x += i.left;
- viewRect.y += i.top;
- viewRect.width -= i.right + viewRect.x;
- viewRect.height -= i.bottom + viewRect.y;
- Rectangle iconRect = new Rectangle();
- Rectangle textRect = new Rectangle();
- Font f = ((Component)c).getFont();
- g.setFont(f);
- String text = SwingUtilities.layoutCompoundLabel(fm, b.getText(), b.getIcon(), b.getVerticalAlignment(), b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect, textRect, b.getText() == null ? 0 : 4);
- this.setTextShiftOffset(0);
- if (model.isArmed() && model.isPressed()) {
- this.paintButtonPressed(g, b);
- }
-
- if (b.getIcon() != null) {
- this.paintIcon(g, c, iconRect);
- }
-
- if (text != null && !text.equals("")) {
- this.paintText(g, c, textRect, text);
- }
-
- if (b.isFocusPainted() && ((JComponent)b).hasFocus()) {
- this.paintFocus(g, b, viewRect, textRect, iconRect);
- }
-
- }
-
- protected void paintButtonPressed(Graphics g, AbstractButton b) {
- }
-
- protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {
- }
-
- protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect) {
- AbstractButton b = (AbstractButton)c;
- ButtonModel model = b.getModel();
- Icon icon = null;
- if (!model.isEnabled()) {
- icon = b.getDisabledIcon();
- } else if (model.isPressed() && model.isArmed()) {
- icon = b.getPressedIcon();
- if (icon == null) {
- icon = b.getIcon();
- } else {
- this.setTextShiftOffset(0);
- }
- } else if (b.isRolloverEnabled() && model.isRollover()) {
- icon = b.getRolloverIcon();
- }
-
- if (icon == null) {
- icon = b.getIcon();
- }
-
- if (model.isPressed() && model.isArmed()) {
- icon.paintIcon(c, g, iconRect.x + this.getTextShiftOffset(), iconRect.y + this.getTextShiftOffset());
- } else {
- icon.paintIcon(c, g, iconRect.x, iconRect.y);
- }
-
- }
-
- protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
- AbstractButton b = (AbstractButton)c;
- ButtonModel model = b.getModel();
- FontMetrics fm = g.getFontMetrics();
- if (model.isEnabled()) {
- g.setColor(((Component)b).getForeground());
- BasicGraphicsUtils.drawString(g, text, model.getMnemonic(), textRect.x + this.getTextShiftOffset(), textRect.y + fm.getAscent() + this.getTextShiftOffset());
- } else {
- g.setColor(((Component)b).getBackground().brighter());
- BasicGraphicsUtils.drawString(g, text, model.getMnemonic(), textRect.x, textRect.y + fm.getAscent());
- g.setColor(((Component)b).getBackground().darker());
- BasicGraphicsUtils.drawString(g, text, model.getMnemonic(), textRect.x - 1, textRect.y + fm.getAscent() - 1);
- }
-
- }
-
- protected void setTextShiftOffset(int i) {
- this.shift_offset = i;
- }
-
- protected void uninstallDefaults(JComponent c) {
- LookAndFeel.uninstallBorder(c);
- }
-
- protected void uninstallKeyboardActions(JComponent c) {
- c.resetKeyboardActions();
- }
-
- protected void uninstallListeners(JComponent c) {
- ((Component)c).removeMouseListener(this.listener);
- ((Component)c).removeMouseMotionListener(this.listener);
- ((Component)c).removeFocusListener(this.listener);
- ((AbstractButton)c).removeChangeListener(this.listener);
- }
-
- public void uninstallUI(JComponent c) {
- this.uninstallKeyboardActions(c);
- this.uninstallListeners(c);
- this.uninstallDefaults(c);
- }
- }
-